/*
 * Copyright 2013 Sigurd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.digiway.rapidbreeze.client.model.config;

import de.digiway.rapidbreeze.client.infrastructure.updatable.UpdatableEntry;
import de.digiway.rapidbreeze.shared.rest.config.ServerConfigurationResourceEntry;
import java.util.Objects;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import org.apache.commons.lang3.ObjectUtils;

/**
 *
 * @author Sigurd
 */
public class ServerConfigurationModel implements UpdatableEntry<ServerConfigurationResourceEntry> {

    private StringProperty key = new SimpleStringProperty();
    private ObjectProperty value = new SimpleObjectProperty();

    public ServerConfigurationModel(String key, Object value) {
        this.key.setValue(key);
        this.value.setValue(value);
    }

    @Override
    public void update(ServerConfigurationResourceEntry entry) {
        if (!ObjectUtils.equals(value.getValue(), entry.getValue())) {
            value.setValue(entry.getValue());
        }
    }

    @Override
    public Object getIdentifier() {
        return key.getValue();
    }

    public void setValue(Object newValue) {
        value.setValue(newValue);
    }

    public <T> T getValue() {
        return (T) value.getValue();
    }

    public String getKey() {
        return key.getValue();
    }

    public Property valueProperty() {
        return value;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 53 * hash + Objects.hashCode(this.key);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ServerConfigurationModel other = (ServerConfigurationModel) obj;
        if (!Objects.equals(this.key, other.key)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "ServerConfigurationModel{" + "key=" + key + ", value=" + value + '}';
    }
}
